// Package cmd /*
package cmd

import (
	httpclient "cmd-tools/http"
	"fmt"
	"github.com/spf13/cobra"
)

func Binding(cmd *cobra.Command) {

	client := httpclient.NewHttpClient()
	if cmd.Flag(httpclient.URL) != nil {
		client.Url(cmd.Flag(httpclient.URL).Value.String())
	}
	if cmd.Flag(httpclient.METHOD) != nil {
		client.Method(cmd.Flag(httpclient.METHOD).Value.String())
	}
	if cmd.Flag(httpclient.CONTENT_TYPE) != nil {
		client.ContentType(cmd.Flag(httpclient.ADD_HEADER).Value.String())
	}
	if cmd.Flag(httpclient.PRINT) != nil {
		client.Printf(cmd.Flag(httpclient.PRINT).Value.String())
	}
	fmt.Println(client.Build())
}

// httpCmd represents the http command
var httpCmd = &cobra.Command{
	Use:   "http",
	Short: "HTTP请求客户端",
	Long:  `支持所有的HTTP请求的客户端`,
	Run: func(cmd *cobra.Command, args []string) {
		Binding(cmd)
		return
	},
}

func init() {
	rootCmd.AddCommand(httpCmd)

	httpCmd.Flags().StringP(httpclient.URL, "u", "http://127.0.0.1", "请求地址")
	httpCmd.Flags().StringP(httpclient.METHOD, "m", "GET", "请求方式")
	httpCmd.Flags().StringP(httpclient.ADD_HEADER, "", "", "添加请求头内容")
	httpCmd.Flags().StringP(httpclient.PRINT, "p", "console", "结果输出的位置")
	httpCmd.Flags().StringP(httpclient.COOKIE, "c", "", "请求携带的cookie")
	httpCmd.Flags().String(httpclient.CONTENT_TYPE, "", "数据格式")
	err := httpCmd.MarkFlagRequired(httpclient.URL)
	if err != nil {
		fmt.Println("请求地址必须填写")
	}
	err = httpCmd.MarkFlagRequired(httpclient.METHOD)
	if err != nil {
		fmt.Println("请求方式必须填写")
	}
}
